package com.usermanage.ServiceImpl;

import com.usermanage.ExceptionConfig.MyException;
import com.usermanage.Mapping.GroupMapping;
import com.usermanage.SecurityConfig.SpringSecurityUtils;
import com.usermanage.SecurityConfig.UserDetail;
import com.usermanage.Service.GroupService;
import com.usermanage.beanParam.GroupBean;
import com.usermanage.beanParam.GroupParam;
import com.usermanage.dataDao.JpaDao;
import com.usermanage.util.JsonData;
import com.usermanage.util.LevelUtil;
import com.usermanage.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Component;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.*;

/**
 * Created by 過客 on 2018/3/9
 */
@Service("groupService")
public class GroupServiceImpl implements GroupService {

    @Resource
    private GroupMapping groupMapping;
    @Resource
    private JpaDao jpaDao;

    @Override
    public List<GroupBean> getUserGroupListByUserId() {
        UserDetail userDetail = SpringSecurityUtils.getCurrentUser();
        List<GroupBean> list = StringUtil.convertListMapToListBean(groupMapping.getUserGroupListByUserId(userDetail.getId().toString()),GroupBean.class);
//        List<GroupBean> result = groupListToTree(list);
        return list;
    }

    @Override
    public List<GroupBean> getAllByCompanyId(String key,String page,String size) {
        StringBuffer buffer = new StringBuffer();
        buffer.append("SELECT * from u_group where company_id = '").append(SpringSecurityUtils.getCurrentUser().getCompany_id()).append("' and group_state = '11' and id != ").append(SpringSecurityUtils.getCurrentUser().getGroup_id());
        if (StringUtils.isNotBlank(key)){
            buffer.append(" and (group_name like '%").append(key).append("%' or group_desc like '%").append(key).append("%')");
        }
        buffer.append(" order by id desc  limit ").append((Integer.valueOf(page)-1) * Integer.valueOf(size)).append(",").append(size);
        List<GroupBean> list = jpaDao.getListByPage(buffer.toString(),GroupBean.class);
        return list;
    }

    @Override
    public int getAllCountByGroupUuid(String key) {
        StringBuffer buffer = new StringBuffer();
        buffer.append("SELECT count(1) from u_group where company_id = '").append(SpringSecurityUtils.getCurrentUser().getCompany_id()).append("' and id != ").append(SpringSecurityUtils.getCurrentUser().getGroup_id());
        if (StringUtils.isNotBlank(key)){
            buffer.append(" and (group_name like '%").append(key).append("%' or group_desc like '%").append(key).append("%')");
        }
        return jpaDao.getListCountByPage(buffer.toString());
    }

    @Override
    public Optional<GroupBean> findById(int id) {
        return groupMapping.findById(id);
    }

    @Override
    public Boolean isExist(GroupBean bean) {
        Example<GroupBean> example = Example.of(bean);
        boolean bool = groupMapping.exists(example);
        return bool;
    }

    @Override
    public GroupBean save(GroupBean bean) {
        return groupMapping.save(bean);
    }

    @Override
    public void deleteById(int id) {
        groupMapping.deleteById(id);
    }

    @Override
    public List<GroupBean> findAllForSelect(GroupBean bean) {
        Example<GroupBean> example = Example.of(bean);
        //根据组id查询
        List<GroupBean> list = groupMapping.findAll(example);
        return list;
    }
//    public List<GroupBean> groupListToTree(List<GroupBean> groupList) {
//        if (CollectionUtils.isEmpty(groupList)) {
//            return Lists.newArrayList();
//        }
//        // level -> [group, group2, ...] Map<String, List<Object>>
//        Multimap<String, GroupBean> groupMap = ArrayListMultimap.create();
//        List<GroupBean> rootList = Lists.newArrayList();
//        for (GroupBean groupBean : groupList) {
//            groupMap.put(groupBean.getLevel(), groupBean);
//            if (LevelUtil.ROOT.equals(groupBean.getLevel())) {
//                rootList.add(groupBean);
//            }
//        }
//        // 递归生成树
//        transformDeptTree(rootList, LevelUtil.ROOT, groupMap);
//        return rootList;
//    }
//
//    // level:0, 0, all 0->0.1,0.2
//    // level:0.1
//    // level:0.2
//    public void transformDeptTree(List<GroupBean> groupList, String level, Multimap<String, GroupBean> groupMap) {
//        for (int i = 0; i < groupList.size(); i++) {
//            // 遍历该层的每个元素
//            GroupBean groupBean = groupList.get(i);
//            // 处理当前层级的数据
//            String nextLevel = LevelUtil.calculateLevel(level, groupBean.getId());
//            // 处理下一层
//            List<GroupBean> list = (List<GroupBean>) groupMap.get(nextLevel);
//            if (CollectionUtils.isNotEmpty(list)) {
//                // 设置下一层部门
//                groupBean.setList(list);
//                // 进入到下一层处理
//                transformDeptTree(list, nextLevel, groupMap);
//            }
//        }
//    }
}
